from __future__ import annotations

from dataclasses import dataclass


@dataclass
class BearingResult:
    ok: bool
    required_area_m2: float
    contact_pressure_kPa: float
    note: str


def check_bearing_capacity(total_load_kN: float, allowable_bearing_kPa: float, base_area_m2: float | None = None) -> BearingResult:
    """地基承载力验算（中心/偏心简化为中心）。"""

    if base_area_m2 is None:
        A_req = total_load_kN * 1000.0 / max(1e-6, allowable_bearing_kPa) / 1000.0
        return BearingResult(ok=True, required_area_m2=A_req, contact_pressure_kPa=allowable_bearing_kPa, note="按承载力反推面积")
    q = total_load_kN / max(1e-6, base_area_m2)
    ok = q * 1000.0 <= allowable_bearing_kPa
    return BearingResult(ok=ok, required_area_m2=base_area_m2, contact_pressure_kPa=q * 1000.0, note="中心荷载验算")


@dataclass
class PunchingShear:
    ok: bool
    capacity_kN: float
    note: str


def check_punching(column_load_kN: float, slab_thickness_mm: float, column_perimeter_mm: float, design_tau_MPa: float = 0.6) -> PunchingShear:
    """基础抗冲切承载力：V_rd ≈ τ·u·h。"""

    Vrd_kN = design_tau_MPa * column_perimeter_mm * (slab_thickness_mm - 30.0) / 1000.0
    ok = column_load_kN <= Vrd_kN
    return PunchingShear(ok=ok, capacity_kN=Vrd_kN, note="简化公式，按规范取τ")


@dataclass
class ShearCapacity:
    ok: bool
    capacity_kN: float
    note: str


def check_shear(Vu_kN: float, b_mm: float, h0_mm: float, design_tau_MPa: float = 0.6) -> ShearCapacity:
    """基础抗剪：V_rd ≈ τ·b·h0。"""

    Vrd = design_tau_MPa * b_mm * h0_mm / 1000.0
    return ShearCapacity(ok=Vu_kN <= Vrd, capacity_kN=Vrd, note="简化抗剪")


@dataclass
class FlexureRebar:
    As_mm2: float
    note: str


def flexural_rebar(Mk_kNm: float, fyd_MPa: float, lever_arm_mm: float) -> FlexureRebar:
    """基础受弯配筋面积 As ≈ M / (fyd·z)。"""

    As = (Mk_kNm * 1e6) / max(1e-6, fyd_MPa * lever_arm_mm)
    return FlexureRebar(As_mm2=As, note="受弯配筋估算")


